/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils.bars;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import utils.code.CodeUtils;
import utils.file.FileUtils;
import utils.info.InfoUtils;
import utils.properties.YourJavaPropertiesUtils;
import utils.system.SystemUtils;
import utils.tabbedpane.TabbedPaneUtils;
import utils.workspace.WorkspaceSelectorUtils;
import views.DictionaryCreator;

/**
 *
 * @author Juan Carlos García
 */
public class ToolBarUtils {

    
        private static JButton yourEclipseButton = new JButton();
        public static JButton createDictionariesButton = new JButton();
        public static JButton editDictionariesButton = new JButton();
        public static JComboBox dictionarySelector = new JComboBox();
        public static JButton newButton = new JButton();
        public static JButton newProjectButton = new JButton();
        public static JButton openButton = new JButton();
        public static JButton changeWorkspaceButton = new JButton();
        public static JButton saveButton = new JButton();
        public static JButton unDoButton = new JButton();
        public static JButton reDoButton = new JButton();
        public static JButton toolsButton = new JButton();
        
    /**
     * This method creates and customize the JToolBar of our System.
     * @return JToolBar
     */
    public static JToolBar getToolBar(final JTabbedPane tabbedPane,final String directory,final JTree fileTree){
        
        JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(false);
        ArrayList dictionaryNames = SystemUtils.getDictionaryNames();

        yourEclipseButton.setToolTipText("Run yourEclipse");
        createDictionariesButton.setToolTipText("Create Dictionary");
        editDictionariesButton.setToolTipText("Edit Dictionary");
        openButton.setToolTipText("Open File");
        newButton.setToolTipText("New File");
        changeWorkspaceButton.setToolTipText("Switch Workspace");
        saveButton.setToolTipText("Save");
        unDoButton.setToolTipText("Undo");
        reDoButton.setToolTipText("Redo");
        toolsButton.setToolTipText("Tools");
        newProjectButton.setToolTipText("Create New Project");
        dictionarySelector.setToolTipText("Select Current Dictionary");
        Dimension dim = new Dimension(220,25);
        dictionarySelector.setPreferredSize(dim);


        try {

            Image imgEclipse = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/eclipseLogo.png"));
            Image imgDictionaryCreator = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/dictionaryCreator.png"));
            Image imgDictionaryEditor = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/dictionaryEditor.png"));
            Image imgOpen = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/open.png")).getScaledInstance(32,32,32);
            Image imgNew = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/new.png"));
            Image imgChange = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/changeWorkspace.png"));
            Image imgSave = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/save.png"));
            Image imgUnDo = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/undo.png"));
            Image imgReDo = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/redo.png"));
            Image imgTools = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/tools.png"));
            Image imgNewProject = ImageIO.read(ToolBarUtils.class.getResource("/resources/img/newProject.png"));

            yourEclipseButton.setIcon(new ImageIcon(imgEclipse));
            createDictionariesButton.setIcon(new ImageIcon(imgDictionaryCreator));
            editDictionariesButton.setIcon(new ImageIcon(imgDictionaryEditor));
            openButton.setIcon(new ImageIcon(imgOpen));
            newButton.setIcon(new ImageIcon(imgNew));
            newProjectButton.setIcon(new ImageIcon(imgNewProject));
            changeWorkspaceButton.setIcon(new ImageIcon(imgChange));
            saveButton.setIcon(new ImageIcon(imgSave));
            unDoButton.setIcon(new ImageIcon(imgUnDo));
            reDoButton.setIcon(new ImageIcon(imgReDo));
            toolsButton.setIcon(new ImageIcon(imgTools));
            dictionarySelector.addItem("<Select your current dictionary>");
            Iterator it = dictionaryNames.iterator();
            while(it.hasNext()){
                dictionarySelector.addItem(it.next());
            }
            String currentDictionary = YourJavaPropertiesUtils.getProperty("currentDictionary","properties");
            dictionarySelector.setSelectedItem(currentDictionary);

        } catch (IOException ex) {

            JOptionPane.showMessageDialog(null,"Error loading image icons\n"+ex.getMessage());
        }

        Dimension dimension = new Dimension(25,25);

        toolBar.add(newButton);
        toolBar.add(newProjectButton);
        toolBar.add(openButton);
        toolBar.add(saveButton);
        toolBar.addSeparator(dimension);
        toolBar.add(changeWorkspaceButton);
        toolBar.addSeparator(dimension);
        toolBar.add(unDoButton);
        toolBar.add(reDoButton);
        toolBar.addSeparator(dimension);
        toolBar.add(toolsButton);
        toolBar.addSeparator(dimension);
        toolBar.add(createDictionariesButton);
        toolBar.add(editDictionariesButton);
        toolBar.add(dictionarySelector);
        toolBar.addSeparator(dimension);
        toolBar.add(yourEclipseButton);

         // Create Info Panels to add in the different Items
        InfoUtils.createInfo(yourEclipseButton,"Run your Code using yourEclipse",20,10,false,Color.decode("#CEECF5"));
        if(dictionaryNames.isEmpty()){
         InfoUtils.createInfo(createDictionariesButton,"Creates your own dictionary! ",20,10,false,Color.decode("#A2644C"));   
        }
        //Listeners
        dictionarySelector.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                int selection = JOptionPane.showConfirmDialog(null,"You're going to use '"+dictionarySelector.getSelectedItem().toString()+"' like your current dictionary, are you sure?");
                if(selection==0){
                   YourJavaPropertiesUtils.setProperty("currentDictionary",dictionarySelector.getSelectedItem().toString(),"properties"); 
                   //Replace Code
                }
            }
        });
        
        createDictionariesButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DictionaryCreator dictionaryEditor = new DictionaryCreator();
            }
        });
        
        newButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                String title = JOptionPane.showInputDialog(null,"Insert the new Class name:");
                
                if(!title.equals("")){
                    
                    TabbedPaneUtils.addTab(tabbedPane,title+".java");
                    tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
                    tabbedPane.setTitleAt(tabbedPane.getSelectedIndex(),title+".java");
                    String codeNewClass = SystemUtils.createNewClass(title);
                
                    JTextPane textArea = (JTextPane) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
                
                    textArea.setText(codeNewClass);
                    
                }
  
            }
        });
        
        /**
         * NewProject ActionListener
         */
        newProjectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                
                String projectName = JOptionPane.showInputDialog(null,"Insert New Project Name");
                
                SystemUtils.createProject(directory,fileTree,projectName);
                
            }
        
        });
        
        
        /**
         * OpenButton ActionListener
         */    
        openButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               
                String route = WorkspaceSelectorUtils.selectAndReturnWorkspace(JFileChooser.FILES_ONLY);
                File file = new File(route);
                TabbedPaneUtils.addTab(tabbedPane,file.getName());
                
                JTextPane textArea = (JTextPane) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
            
                String code = CodeUtils.replaceCodeOnOpen(file);
            
                textArea.setText(code);
               
            }
        });
       
        /**
         * ChangeWorkspace ActionListener
         */ 
        changeWorkspaceButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                
                WorkspaceSelectorUtils.select();
                
            }
        });
        
        /**
         * SaveButton ActionListener
         */
          saveButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JTextPane textArea = (JTextPane) tabbedPane.getComponentAt(tabbedPane.getSelectedIndex());
                String title = tabbedPane.getTitleAt(tabbedPane.getSelectedIndex());
                String code = CodeUtils.replaceCodeOnSave(textArea.getText());
                FileUtils.saveFile(directory,title,code,fileTree);
                
            }
        });

        return toolBar;
        

    }
    
}
